/*
 * Copyright [duowan.com]
 * Web Site: http://www.duowan.com
 * Since 2005 - 2014
 */

package com.duowan.realtime.dao.impl;

import static com.duowan.common.util.ObjectUtils.isNotEmpty;

import java.util.List;

import org.springframework.dao.support.DataAccessUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;

import com.duowan.common.util.page.Page;
import com.duowan.realtime.dao.BaseSpringJdbcDao;
import com.duowan.realtime.dao.ServerInfoDao;
import com.duowan.realtime.model.ServerInfo;
import com.duowan.realtime.query.ServerInfoQuery;

/**
 * tableName: server_info
 * [ServerInfo] 的Dao操作 
 *  
 * @author badqiu email:badqiu(a)gmail.com
 * @version 1.0
 * @since 1.0
*/
public class ServerInfoDaoImpl extends BaseSpringJdbcDao implements ServerInfoDao{
	
	private RowMapper<ServerInfo> entityRowMapper = new BeanPropertyRowMapper<ServerInfo>(getEntityClass());
	
	static final private String COLUMNS = "id,realtime_computing_group,ip";
	static final private String SELECT_FROM = "select " + COLUMNS + " from server_info";
	
	@Override
	public Class<ServerInfo> getEntityClass() {
		return ServerInfo.class;
	}
	
	@Override
	public String getIdentifierPropertyName() {
		return "id";
	}
	
	public RowMapper<ServerInfo> getEntityRowMapper() {
		return entityRowMapper;
	}
	
	public void insert(ServerInfo entity) {
		String sql = "insert into server_info " 
			 + " (id,realtime_computing_group,ip) " 
			 + " values "
			 + " (:id,:realtimeComputingGroup,:ip)";
		insertWithGeneratedKey(entity,sql); //for sqlserver:identity and mysql:auto_increment
		
		//其它主键生成策略
		//insertWithOracleSequence(entity,"sequenceName",sql); //oracle sequence: 
		//insertWithDB2Sequence(entity,"sequenceName",sql); //db2 sequence:
		//insertWithUUID(entity,sql); //uuid
		//insertWithAssigned(entity,sql); //手工分配
	}
	
	public int update(ServerInfo entity) {
		String sql = "update server_info set "
					+ " realtime_computing_group=:realtimeComputingGroup,ip=:ip "
					+ " where  id = :id ";
		return getNamedParameterJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(entity));
	}
	
	public int deleteById(int id) {
		String sql = "delete from server_info where  id = ? ";
		return  getSimpleJdbcTemplate().update(sql,  id);
	}

	public ServerInfo getById(int id) {
		String sql = SELECT_FROM + " where  id = ? ";
		return (ServerInfo)DataAccessUtils.singleResult(getSimpleJdbcTemplate().query(sql, getEntityRowMapper(),id));
	}
	

	public Page<ServerInfo> findPage(ServerInfoQuery query) {
		
		StringBuilder sql = new StringBuilder("select "+ COLUMNS + " from server_info where 1=1 ");
		if(isNotEmpty(query.getId())) {
            sql.append(" and id = :id ");
        }
		if(isNotEmpty(query.getBloomfilterGroup())) {
            sql.append(" and bloomfilter_group = :bloomfilterGroup ");
        }
		if(isNotEmpty(query.getIp())) {
            sql.append(" and ip = :ip ");
        }
		
        //sql.append(" order by :sortColumns ");
		
		return pageQuery(sql.toString(),query,getEntityRowMapper());				
	}

	@Override
	public List<ServerInfo> getAll() {
		String sql = SELECT_FROM + " where  1 = 1 ";
		return getJdbcTemplate().query(sql, getEntityRowMapper());
	}

	@Override
	public ServerInfo getServerIps(String realtimeComputingGroup) {
		String sql = SELECT_FROM + " where realtime_computing_group=?"; 
		return (ServerInfo)DataAccessUtils.singleResult(getSimpleJdbcTemplate().query(sql, getEntityRowMapper(),realtimeComputingGroup));
	}
}
